package com.sukaiyi.giteeideaplugin.mainwindow;

import cn.hutool.core.util.StrUtil;
import com.intellij.idea.LoggerFactory;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowFactory;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.ui.content.ContentManager;
import com.intellij.util.messages.MessageBus;
import com.sukaiyi.giteeideaplugin.component.LoadingContainer;
import com.sukaiyi.giteeideaplugin.search.repo.GiteeRepoSearchEverywhereContributorFactoryJavassistGenerator;
import com.sukaiyi.giteeideaplugin.setting.AccessTokenChangeNotifier;
import com.sukaiyi.giteeideaplugin.setting.PluginSetting;
import com.sukaiyi.giteejavaapi.common.Gitee;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

/**
 * @author sukaiyi
 * @date 2020/06/30
 */
public class GiteeMainWindowFactory implements ToolWindowFactory {

    static {
        Logger.setFactory(LoggerFactory.class);
    }

    static {
        GiteeRepoSearchEverywhereContributorFactoryJavassistGenerator.exec();
    }

    static {
        PluginSetting setting = PluginSetting.getInstance();
        Gitee.getInstance().setAccessToken(setting.getGiteeAccessToken());
    }

    private ToolWindow toolWindow;

    public GiteeMainWindowFactory() {
        MessageBus bus = ApplicationManager.getApplication().getMessageBus();
        bus.connect().subscribe(AccessTokenChangeNotifier.TOPIC, (before, after) -> {
            if (toolWindow == null) {
                return;
            }
            Gitee.getInstance().setAccessToken(after);
            ContentManager contentManager = toolWindow.getContentManager();
            contentManager.removeAllContents(true);
            ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
            if (StrUtil.isEmpty(after)) {
                GuideToAccessTokenWindow window = new GuideToAccessTokenWindow(toolWindow);
                Content content = contentFactory.createContent(window.getContent(), "Guide", false);
                contentManager.addContent(content);
            } else {
                createContents(contentFactory).forEach(contentManager::addContent);
            }
        });
    }

    @Override
    public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        this.toolWindow = toolWindow;

        PluginSetting setting = PluginSetting.getInstance();
        String accessToken = setting.getGiteeAccessToken();

        ContentManager contentManager = toolWindow.getContentManager();
        ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();
        if (StrUtil.isEmpty(accessToken)) {
            GuideToAccessTokenWindow window = new GuideToAccessTokenWindow(toolWindow);
            Content content = contentFactory.createContent(window.getContent(), "Guide", false);
            contentManager.addContent(content);
        } else {
            createContents(contentFactory).forEach(contentManager::addContent);
        }
    }

    private List<Content> createContents(ContentFactory contentFactory) {
        return Arrays.asList(
                contentFactory.createContent(new LoadingContainer(new CodeSegmentWindow(toolWindow)), "Code Segments", false),
                contentFactory.createContent(new LoadingContainer(new PersonalWindow(toolWindow)), "Workspace", false)
        );
    }
}
